package com.agrm.user.controller;

import com.wf.captcha.SpecCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.Duration;

@RestController
@RequestMapping(value = "/api/v1/user/captcha")
public class CaptchaApi {
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 验证码
     */
    @GetMapping
    public void captcha(String id, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("hhh");
        SpecCaptcha captcha = new SpecCaptcha(140, 40, 4);
        resp.setContentType("image/gif");
        resp.setHeader("Pragma", "No-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        //req.getSession().setAttribute("captcha", captcha.text().toLowerCase());
//        redisTemplate.opsForValue().set("captcha-" + id, captcha.text().toLowerCase(), Duration.ofMinutes(2));
        captcha.out(resp.getOutputStream());
    }

}